package buisness_service;

import java.util.Calendar;

public class Depense extends BuisnessEntity {
	private PosteDepense posteDepense;
	private Depense_jour depense_jour;

	public Depense(float montant1, Calendar date1, User user) {
		super(montant1, date1, user);

		this.getUser().addDepense(this);

		this.verif_ancetre(date1, user);
	}

	public Depense(String nom, float montant1, Calendar date1,
			PosteDepense posteDepense, User user) {
		super(montant1, date1, user);
		this.setNom(nom);
		this.getUser().addDepense(this);
		this.verif_ancetre(date1, user);
		this.posteDepense = posteDepense;
		this.posteDepense.getDepenses().add(this);

	}

	public PosteDepense getPosteDepense() {
		return posteDepense;
	}

	public void setPosteDepense(PosteDepense posteDpense) {
		this.posteDepense = posteDpense;
	}

	public Depense_jour getDepense_jour() {
		return depense_jour;
	}

	public void setDepense_jour(Depense_jour depense_jour) {
		this.depense_jour = depense_jour;
	}

	public void verif_ancetre(Calendar date1, User u ) {
		if (Rapport.rechercher_jour(date1.get(1), Rapport.tab[date1.get(2)],
				date1.get(5), u.getRapport()) == false) {
			if (Rapport.index_annee >= 0) {
				if (Rapport.index_mois >= 0) {

					Finance_jour fin_jour = new Finance_jour(date1.get(5),
							u.getRapport().get(Rapport.index_annee)
									.getFinances_mois().get(Rapport.index_mois), u);

					 this.depense_jour= fin_jour.getDep_jour();
				} else {
					Finance_mois finance = new Finance_mois(
							Rapport.tab[date1.get(2)],
							u.getRapport().get(Rapport.index_annee), u);
					Finance_jour fin_jour = new Finance_jour(date1.get(5),
							finance, u);

					this.depense_jour = fin_jour.getDep_jour();
				}

			} else {

				Finance_annee f_a = new Finance_annee(this.getDate().get(1), u.getRapport());

				Finance_mois finance = new Finance_mois(
						Rapport.tab[date1.get(2)], f_a, u);
				Finance_jour fin_jour = new Finance_jour(date1.get(5), finance,u);

				this.depense_jour = fin_jour.getDep_jour();

			}
		} else {

			this.depense_jour = u.getRapport().get(Rapport.index_annee)
					.getFinances_mois().get(Rapport.index_mois)
					.getFinances_jour().get(Rapport.index_jour).getDep_jour();
		}

		u.getRapport()
				.get(this.depense_jour.getFinance_jour().getFinance_mois()
						.getIndexFinance_annee())
				.getFinances_mois()
				.get(this.depense_jour.getFinance_jour().getFinance_mois()
						.getIndex_finance_mois())
				.getFinances_jour()
				.get(this.depense_jour.getFinance_jour()
						.getIndex_finance_jour()).getDep_jour().getDepenses()
				.add(this);

	}

}
